﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

using MyLibrary.Json;
using MyLibrary.Data.Builder;
using MyLibrary.Data.Builder.Config;

namespace MyLibrary.Data.Builder.Component
{
    class ConditionDropDown : ConditionComponentBase
    {
        private string _dbConnKey = String.Empty;
        /// <summary>
        /// 报表数据库的连接字符串键名
        /// </summary>
        public override string DBConnectionKey
        {
            get { return _dbConnKey; }
            set { _dbConnKey = value; }
        }

        private ReportCondition _condition = null;
        /// <summary>
        /// 下拉框中的数据源类型
        /// </summary>
        public override ReportCondition ReportCondition
        {
            get { return _condition; }
            set { _condition = value; }
        }

        /// <summary>
        /// 添加ASP控件
        /// </summary>
        /// <param name="holder">控件容器</param>
        public override void DrawASPComponent(PlaceHolder holder)
        {
            Label label = new Label();
            if (!String.IsNullOrEmpty(_condition.ConditionTitle))
            {
                label.Text = "&nbsp;" + _condition.ConditionTitle + "：";
                holder.Controls.Add(label);
            }
            DropDownList dropDown = new DropDownList();
            switch (_condition.ConditionSource)
            {
                case "database":
                    AddDropDownItemWithDatabase(dropDown);
                    break;
                case "json":
                    AddDropDownItemWithJson(dropDown);
                    break;
            }
            if (!String.IsNullOrEmpty(_condition.ConditionID))
                dropDown.ID = _condition.ConditionID;
            else
                dropDown.ID = Guid.NewGuid().ToString();
            holder.Controls.Add(dropDown);
        }

        /// <summary>
        /// 添加HTML控件
        /// </summary>
        public override string DrawHTMLComponent()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 添加数据源来数据库的下拉框
        /// </summary>
        private void AddDropDownItemWithDatabase(DropDownList dropDown)
        {
            Dictionary<string, string> src = base.GetSourceFromDb(_dbConnKey, _condition.ConditionContent);
            foreach (KeyValuePair<string, string> pair in src)
            {
                ListItem item = new ListItem();
                item.Value = pair.Key;
                item.Text = pair.Value;
                dropDown.Items.Add(item);
            }
            if(dropDown.Items.Count > 0)
                dropDown.Items[0].Selected = true;
        }

        /// <summary>
        /// 添加数据源来自Json串
        /// </summary>
        /// <param name="dropDown"></param>
        private void AddDropDownItemWithJson(DropDownList dropDown)
        {
            Dictionary<string, string> src = new Dictionary<string, string>();
            string key, value = String.Empty;
            using (JsonReader jsonReader = new JsonTextReader(new StringReader(_condition.ConditionContent)))
            {
                while (jsonReader.Read())
                {
                    if (jsonReader.TokenType == JsonToken.StartObject)
                        continue;
                    else if (jsonReader.TokenType == JsonToken.PropertyName)
                    {
                        key = jsonReader.Value.ToString();
                        jsonReader.Read();
                        if (jsonReader.TokenType == JsonToken.String)
                            value = jsonReader.Value.ToString();
                        else
                            continue;
                        src.Add(key, value);
                    }
                }
            }
            //添加到下拉菜单中
            foreach (KeyValuePair<string, string> pair in src)
            {
                ListItem item = new ListItem();
                item.Value = pair.Key;
                item.Text = pair.Value;
                dropDown.Items.Add(item);
            }
            if (dropDown.Items.Count > 0)
                dropDown.Items[0].Selected = true;
        }
    }
}
